/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-27     AlgoIdeas       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#include <string.h>
#include <stdio.h>
#include <time.h>

#include "page.h"
#include "gps.h"
#include "ssd1306.h"

static const char *WDAY_NAMES[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};  //星期
static const char *MONTH_NAMES[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};  //月份
static page_type_t page_index = PAGE_STARTUP;

/* GPS location distance */
static rt_uint8_t first_location = 0;
static float preLong;
static float preLat;


static void date_time_update(int param)
{
    time_t now = time(NULL);
    struct tm* timeInfo;
    timeInfo = localtime(&now);
    char buff[16];

    ssd1306_Fill(Black);
    ssd1306_SetCursor(0, 0);
    if (param) {
        ssd1306_WriteString("rec", Font_6x8, White);
    }

    /* 记录第一次定位的坐标 */
    if (0 == first_location) {
        first_location = 1;

        GPS_Info_t info;
        gps_get_info(&info);
        preLong = info.longitude;
        preLat = info.latitude;
    }

    sprintf(buff, "%04d-%02d-%02d  %s", timeInfo->tm_year + 1900, timeInfo->tm_mon + 1,
            timeInfo->tm_mday, WDAY_NAMES[timeInfo->tm_wday]);
    ssd1306_SetCursor(10, 10);
    ssd1306_WriteString(buff, Font_11x18, White);

    sprintf(buff, "%02d:%02d:%02d", timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
    ssd1306_SetCursor(21, 30);
    ssd1306_WriteString(buff, Font_11x18, White);
    ssd1306_UpdateScreen();
}

static void location_update()
{
    GPS_Info_t info;
    char buff[16];

    ssd1306_Fill(Black);
    ssd1306_SetCursor(0, 0);
    ssd1306_WriteString("Long:", Font_6x8, White);

    gps_get_info(&info);
    sprintf(buff, "%.6f", info.longitude);
    ssd1306_SetCursor(12, 8);
    ssd1306_WriteString(buff, Font_11x18, White);

    ssd1306_SetCursor(0, 26);
    ssd1306_WriteString("Lati:", Font_6x8, White);

    sprintf(buff, "%.6f", info.latitude);
    ssd1306_SetCursor(12, 34);
    ssd1306_WriteString(buff, Font_11x18, White);
    ssd1306_UpdateScreen();
}

static void speed_update()
{
    GPS_Info_t info;
    char buff[16];

    gps_get_info(&info);
    sprintf(buff, "%.2f km/h", info.speed);
    ssd1306_Fill(Black);
    ssd1306_SetCursor(0, 10);
    ssd1306_WriteString(buff, Font_11x18, White);

    sprintf(buff, "%.2f m", gps_get_distance(&info, preLong, preLat));
    ssd1306_SetCursor(0, 32);
    ssd1306_WriteString(buff, Font_11x18, White);

    ssd1306_UpdateScreen();
}

static void startup_update(int param)
{
    static int rotate = 0;
    char rotate_list[] = { '*', '|', '/', '-', '\\' };
    char buff[16];

    rotate = (rotate + 1) % 5;

    ssd1306_Fill(Black);

    sprintf(buff, "%c", rotate_list[rotate]);
    ssd1306_SetCursor(0, 0);
    ssd1306_WriteString(buff, Font_7x10, White);

    sprintf(buff, "%s", param ? "  Located  " : "Locating ...");
    ssd1306_SetCursor(21, 30);
    ssd1306_WriteString(buff, Font_7x10, White);
    ssd1306_UpdateScreen();

    return ;
}

void page_init()
{
    ssd1306_Init();
    return ;
}

void page_switch(rt_int32_t index)
{
    page_index = index;
    return ;
}

void page_update(page_state_t state)
{
    switch (page_index) {
        case PAGE_STARTUP:
            startup_update((state & PAGE_STATE_LOCATION));
            break;
        case PAGE_DATE_TIME:
            date_time_update((state & PAGE_STATE_TRACK));
            break;
        case PAGE_LOCATION:
            location_update();
            break;
        case PAGE_SPEED:
            speed_update();
            break;
    }

    return ;
}


